package net.net23.sysdent.client.ui.widgets.links;

import java.util.List;

import net.net23.sysdent.client.services.LinksService;
import net.net23.sysdent.client.services.LinksServiceAsync;
import net.net23.sysdent.client.ui.widgets.dialog.T4LDialog;
import net.net23.sysdent.client.ui.widgets.dialog.T4LDialogHandler;
import net.net23.sysdent.client.ui.widgets.model.SearcherWidget;
import net.net23.sysdent.client.ui.widgets.search.ResultTable;
import net.net23.sysdent.shared.jto.Link;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LinksSearcher extends SearcherWidget implements T4LDialogHandler {

	private Label resultLabel;
	private TextBox inputSearch;
	LinksServiceAsync reloadService = GWT.create(LinksService.class);
	protected List<Link> currentList;

	private DialogBox dialogBox;

	public LinksSearcher(String key, ImageResource icon) {
		super(key, icon);
	}

	@Override
	public Widget getCriteriaWidget() {
		FlexTable layout = new FlexTable();
		layout.setWidth("100%");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
		layout.setStyleName("t4l-seacher-criteria");

		cellFormatter.setColSpan(0, 0, 3);
		resultLabel = new Label("");

		HorizontalPanel searchField = new HorizontalPanel();
		searchField.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		Button add = new Button("+", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showAddLinkForm();
			}
		});
		searchField.add(add);

		inputSearch = new TextBox();
		inputSearch.addStyleName("criteria-widget-search");
		inputSearch.setWidth("200px");
		inputSearch.setHeight("23px");
		inputSearch.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					performSearch();
				}
			}
		});

		Button buttonSearch = new Button("Buscar");
		searchField.add(inputSearch);
		buttonSearch.setHeight("23px");
		buttonSearch.addStyleName("criteria-widget-search");
		searchField.add(buttonSearch);
		buttonSearch.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				performSearch();
			}
		});

		layout.setWidget(0, 0, resultLabel);
		cellFormatter.setAlignment(0, 3, HasHorizontalAlignment.ALIGN_RIGHT,
				HasVerticalAlignment.ALIGN_MIDDLE);
		layout.setWidget(0, 3, searchField);

		return layout;

	}

	protected void showAddLinkForm() {
		EditLinkForm editLinkForm = new EditLinkForm(this, reloadService, null);
		dialogBox = T4LDialog.getDialogBox("Crear nuevo link", editLinkForm,
				false);
		dialogBox.show();
	}

	public void performSearch() {
		freeze();
		reloadService.search(inputSearch.getText(),
				new AsyncCallback<List<Link>>() {
					@Override
					public void onSuccess(List<Link> result) {
						defreeze();
						getResult().removeAllRows();
						LinksSearcher.this.currentList = result;
						int index = 0;
						for (Link link : result) {
							getResult().setWidget(index++, 0,
									new T4LLinkItem(reloadService, link));
						}
						resultLabel.setText(index + " Enlaces encontrados.");
						// dialogBox.hide();
					}

					@Override
					public void onFailure(Throwable caught) {
						defreeze();
						Window.alert(caught.getMessage());
					}
				});
	}

	@Override
	public ResultTable getResultWidget() {
		ResultTable results = new ResultTable();
		results.setWidth("100%");
		return results;
	}

	@Override
	public void close() {
		if (dialogBox != null) {
			dialogBox.hide();
			dialogBox = null;
		}
	}
}
